﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BankSite.Sites.Deposits
{
    public partial class NewDeposit : System.Web.UI.Page
    {
        private const int NOT_YET_SELECTED = -1;
        private int selectedDepositTypeId = NOT_YET_SELECTED;
        private int selectedDurationTimeId = NOT_YET_SELECTED;

        protected void Page_Load(object sender, EventArgs e)
        {
            TextBox startDateTB = this.NewDepositFV.FindControl("data_zalozeniaTextBox") as TextBox;
            if (startDateTB != null)
            {
                startDateTB.Text = DateTime.Now.ToShortDateString();
            }
            
            //DepositTypeDDL_SelectedIndexChanged(sender, e);
            //DepositTimeDDL_SelectedIndexChanged(sender, e);
        }

        protected void NewDepositFormView_DataBound(object sender, EventArgs e)
        {

        }

        protected void CurrencyDropdownEDS_QueryCreated(object sender, QueryCreatedEventArgs e)
        {
            var Currencies = e.Query.Cast<Waluty>();
            e.Query = from currency in Currencies where currency.id == 120 select currency;  // jak na razie limitujemy do PLN; będzie trzeba zrobić rozliczanie międzywalutowe
        }

        protected void DepositDurationEDS_QueryCreated(object sender, QueryCreatedEventArgs e)
        {
            var Durations = e.Query.Cast<vOkresyLokatDropDown>();
            e.Query = from duration in Durations where duration.id_rodzaj_lokaty == selectedDepositTypeId select duration;  
        }

        protected void ComputeEndDate(object sender, EventArgs e)
        {
            TextBox endDateTB = this.NewDepositFV.FindControl("data_zakonczeniaTextBox") as TextBox;
            if (endDateTB != null)
            {
                using (var context = new BankBaseEntities())
                {
                    if (this.selectedDurationTimeId != -1)
                    {
                        Okresy_lokat okres = context.Okresy_lokat.Where(u => u.id == (short)this.selectedDurationTimeId).First();

                        int daysToAdd = okres.ilosc_dni; // tu dorobić zasysanie z bazy ile dni ma wybrany przez uska okres
                        endDateTB.Text = DateTime.Now.AddDays(daysToAdd).ToShortDateString();
                    }
                }
            }
        }

        protected void DepositTypeDDL_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList depTypeDDL = this.NewDepositFV.FindControl("DepositTypeDDL") as DropDownList;
            if (depTypeDDL != null)
            {
                if (!Int32.TryParse(depTypeDDL.SelectedValue, out selectedDepositTypeId))
                {
                    selectedDepositTypeId = NOT_YET_SELECTED;
                }
                else
                {
                    DropDownList depTimeDDL = this.NewDepositFV.FindControl("DepositTimeDDL") as DropDownList;
                    if (depTimeDDL != null)
                    {
                        depTimeDDL.DataBind();

                        //to zawołanie wywoła przetwarzanie cuda od aktualnej daty
                        DepositTimeDDL_SelectedIndexChanged(sender, e);
                    }
                }
            }
        }

        protected void DepositTimeDDL_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList depTimeDDL = this.NewDepositFV.FindControl("DepositTimeDDL") as DropDownList;
            if (depTimeDDL != null)
            {
                if (!Int32.TryParse(depTimeDDL.SelectedValue, out selectedDurationTimeId))
                {
                    selectedDurationTimeId = NOT_YET_SELECTED;
                }
                ComputeEndDate(sender, e);
            }
            
        }

        protected void NewDepositEDS_Inserted(object sender, EntityDataSourceChangedEventArgs e)
        {
            //tu można zebrać ID utworzonej encji z bazki. Hah haa!
        }

        /// <summary>
        /// Przetwarzanie checkboxów - checkbox dopisywania do lokaty nie może być aktywny ani zaznaczony, jeśli nie jest aktywny checkbox od odnawiania lokaty
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void odnawialnaCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox renewableChkBox = this.NewDepositFV.FindControl("odnawialnaCheckBox") as CheckBox;
            CheckBox accumulativeChkBox = this.NewDepositFV.FindControl("accumulativeCheckBox") as CheckBox;
            if (renewableChkBox != null && accumulativeChkBox != null)
            {
                if (renewableChkBox.Checked)
                {
                    accumulativeChkBox.Enabled = true;
                }
                else
                {
                    accumulativeChkBox.Enabled = false;
                    accumulativeChkBox.Checked = false;
                }
            }
        }
    }
}